/*分页传值*/
$(function () {
    /*点击页码跳转到相应的页面*/
    $(".pageNumber").css("color", "#FFAD00");
    //点击页码
    $(document).on("click",".pageNumber",function () {
        //把所有的变回原来的颜色
        $(".pageNumber").css("color", "#FFAD00");
        $(this).css("color", "#37167C");
        //获得当前页面数
        var num = $(this).text();
        //请求后台
        $.ajax({
            url: "/petMart/petMartList",
            type: "post",
            dataType: "json",
            data: {"page": num},
            success: function (data) {
                $("#listPro").empty();
                if (num < 6) {
                    $.each(data.list, function (i, obj) {
                        $("#listPro").append("<li class='jumpProduces'>" +
                            "<a><img src='" + obj.image + "' width='140' height='250' alt='Pet Shop' title='Pet Shop'/></a>" +
                            "<h2>"+obj.proName+"</h2>" +
                            "<span style='display: none'>"+obj.id+"</span>" +
                            "<span>"+parseFloat(obj.price)+".0"+"</span>" +
                            "<span>"+obj.text+"</span>" +
                            "<span><a class='more'>View all</a></span>" +
                            "</li>");
                    });

                } else {
                    //获得总页数
                    var countPage = data.countPage.length;
                    $("#pages").empty();
                    $("#listPro").empty();
                    //添加上一页下一页
                    $("#pages").append("<li>" +
                        "<a id='front' style='color: #FFAD00'>上一页</a>" +
                        "</li><li>" +
                        "<a id='next' style='color: #FFAD00'>下一页</a>" +
                        "</li>");

                    $.each(data.list, function (i, obj) {
                        $("#listPro").append("<li class='jumpProduces'>" +
                            "<a><img src='" + obj.image + "' width='140' height='250' alt='Pet Shop' title='Pet Shop'/></a>" +
                            "<h2>"+obj.proName+"</h2>" +
                            "<span style='display: none'>"+obj.id+"</span>" +
                            "<span>"+parseFloat(obj.price)+".0"+"</span>" +
                            "<span>"+obj.text+"</span>" +
                            "<span><a class='more'>View all</a></span>" +
                            "</li>");
                    });

                    //求出当前点击页码的对5的余数
                    var res = num%5;
                    if(res == 1){
                        if (num + 4 < countPage) {
                            //页码
                            for (var b = num; b <= num + 5; b++) {
                                if(b == num){
                                    $("#next").parent().before(
                                        "<li>" +
                                        "<a class='pageNumber' style='color: #37167C'>" + b + "</a>" +
                                        "</li>");
                                }else {
                                    $("#next").parent().before(
                                        "<li>" +
                                        "<a class='pageNumber' style='color: #FFAD00'>" + b + "</a>" +
                                        "</li>");
                                }

                            }
                        } else {
                            //页码
                            for (var b = num; b <= countPage; b++) {
                                if(b == num){
                                    $("#next").parent().before(
                                        "<li>" +
                                        "<a class='pageNumber' style='color: #37167C'>" + b + "</a>" +
                                        "</li>");
                                }else {
                                    $("#next").parent().before(
                                        "<li>" +
                                        "<a class='pageNumber' style='color: #FFAD00'>" + b + "</a>" +
                                        "</li>");
                                }
                            }
                        }
                    }else if(res == 0){
                        //页码
                        for (var b = num-4; b <= num; b++) {
                            if(b == num){
                                $("#next").parent().before(
                                    "<li>" +
                                    "<a class='pageNumber' style='color: #37167C'>" + b + "</a>" +
                                    "</li>");
                            }else {
                                $("#next").parent().before(
                                    "<li>" +
                                    "<a class='pageNumber' style='color: #FFAD00'>" + b + "</a>" +
                                    "</li>");
                            }
                        }
                    }else{
                        //增加新页码
                        if(parseInt(num)+5-res<=countPage){
                            for (var b = num-res+1; b <= parseInt(num)+5-res; b++) {
                                if(b == num){
                                    $("#next").parent().before(
                                        "<li>" +
                                        "<a class='pageNumber' style='color: #37167C'>" + b + "</a>" +
                                        "</li>");
                                }else {
                                    $("#next").parent().before(
                                        "<li>" +
                                        "<a class='pageNumber' style='color: #FFAD00'>" + b + "</a>" +
                                        "</li>");
                                }
                            }
                        }else {
                            for (var b = num-res+1; b <= countPage; b++) {
                                if(b == num){
                                    $("#next").parent().before(
                                        "<li>" +
                                        "<a class='pageNumber' style='color: #37167C'>" + b + "</a>" +
                                        "</li>");
                                }else {
                                    $("#next").parent().before(
                                        "<li>" +
                                        "<a class='pageNumber' style='color: #FFAD00'>" + b + "</a>" +
                                        "</li>");
                                }
                            }
                        }
                    }
                }
            }
        })
    });
});

/*点击上一页下一页*/
$(function () {
    //点击上一页
    $(document).on("click","#front",function () {
        if (($(this).parent().next().text().trim()) > 1) {
            var num = $(this).parent().next().text().trim() - 1;
            //请求后台
            $.ajax({
                url: "/petMart/petMartList",
                type: "post",
                dataType: "json",
                data: {"page": num},
                success: function (data) {
                    $("#listPro").empty();
                    //获得总页数
                    var countPage = data.countPage.length;
                    //清空数据
                    $("#pages").empty();
                    //添加上一页下一页
                    $("#pages").append("<li>" +
                        "<a id='front' style='color: #FFAD00'>上一页</a>" +
                        "</li><li>" +
                        "<a id='next' style='color: #FFAD00'>下一页</a>" +
                        "</li>");

                    $("#listPro").empty();
                    $.each(data.list, function (i, obj) {
                        $("#listPro").append("<li class='jumpProduces'>" +
                            "<a><img src='" + obj.image + "' width='140' height='250' alt='Pet Shop' title='Pet Shop'/></a>" +
                            "<h2>"+obj.proName+"</h2>" +
                            "<span style='display: none'>"+obj.id+"</span>" +
                            "<span>"+parseFloat(obj.price)+".0"+"</span>" +
                            "<span>"+obj.text+"</span>" +
                            "<span><a class='more'>View all</a></span>" +
                            "</li>");
                    });
                    //页码赋值
                    for (var b = num-4; b <= num; b++) {
                        $("#next").parent().before(
                            "<li>" +
                            "<a class='pageNumber'>" + b + "</a>" +
                            "</li>");
                    }
                    $(".pageNumber").css("color", "#FFAD00");
                }
            })
        } else {
            alert("这已经是第一页啦！");
        }
    });

    //点击下一页
    $(document).on("click","#next",function () {
        var num = parseInt($(this).parent().prev().text().trim());
            //请求后台
            $.ajax({
                url: "/petMart/petMartList",
                type: "post",
                dataType: "json",
                data: {"page": num},
                success: function (data) {
                    var countPage = data.countPage.length;
                    if (num+1 <= countPage) {
                        $("#listPro").empty();
                        //获得总页数
                        $("#pages").empty();
                        //添加上一页下一页
                        $("#pages").append("<li>" +
                            "<a id='front' style='color: #FFAD00'>上一页</a>" +
                            "</li><li>" +
                            "<a id='next' style='color: #FFAD00'>下一页</a>" +
                            "</li>");
                        $("#listPro").empty();
                        $.each(data.list, function (i, obj) {
                            $("#listPro").append("<li class='jumpProduces'>" +
                                "<a><img src='" + obj.image + "' width='140' height='250' alt='Pet Shop' title='Pet Shop'/></a>" +
                                "<h2>"+obj.proName+"</h2>" +
                                "<span style='display: none'>"+obj.id+"</span>" +
                                "<span>"+parseFloat(obj.price)+".0"+"</span>" +
                                "<span>"+obj.text+"</span>" +
                                "<span><a class='more'>View all</a></span>" +
                                "</li>");
                        });

                        if (num + 5 < countPage) {
                            //页码
                            for (var b = num; b <= num + 5; b++) {
                                $("#next").parent().before(
                                    "<li>" +
                                    "<a class='pageNumber' >" + b + "</a>" +
                                    "</li>");
                            }
                        } else {
                            //页码
                            for (var b = num+1; b <= countPage; b++) {
                                $("#next").parent().before(
                                    "<li>" +
                                    "<a class='pageNumber'>" + b + "</a>" +
                                    "</li>");
                            }
                        }
                        $(".pageNumber").css("color", "#FFAD00");
                    } else {
                        alert("这是最后一页啦！");
                    }
                }
            })
    });
})

/*跳转商品详情*/
$(function () {
    $(document).on("click",".jumpProduces",function () {
        //获得点击当前商品的id
        var id = $(this).children().eq(2).text();
        window.location.href = "/petMart/jumpProduces?id="+id;
    })

    $(document).on("click",".more",function () {
        //获得点击当前商品的id
        var id = $(this).parent().parent().children().eq(2).text();
        window.location.href = "/petMart/jumpProduces?id="+id;
    })
})